package com.github.jspxnet.worktask.view;

import com.github.jspxnet.boot.res.LanguageRes;
import com.github.jspxnet.sioc.annotation.Ref;
import com.github.jspxnet.sober.TableModels;
import com.github.jspxnet.txweb.AssertException;
import com.github.jspxnet.txweb.annotation.Describe;
import com.github.jspxnet.txweb.annotation.Operate;
import com.github.jspxnet.txweb.annotation.Param;
import com.github.jspxnet.txweb.annotation.PathVar;
import com.github.jspxnet.txweb.model.param.PageParam;
import com.github.jspxnet.txweb.result.RocResponse;
import com.github.jspxnet.txweb.support.ActionSupport;
import com.github.jspxnet.txweb.table.CloudFileConfig;
import com.github.jspxnet.utils.BeanUtil;
import com.github.jspxnet.utils.StringUtil;
import com.github.jspxnet.worktask.dao.ConfWorkFlowDAO;
import com.github.jspxnet.worktask.env.JWorkTask;
import com.github.jspxnet.worktask.model.dto.ConfFlowJobDto;
import com.github.jspxnet.worktask.model.dto.ConfFlowNodeDto;
import com.github.jspxnet.worktask.model.dto.ConfWorkFlowDto;
import com.github.jspxnet.worktask.model.param.ConfFlowNodeParam;
import com.github.jspxnet.worktask.table.ConfFlowJob;
import com.github.jspxnet.worktask.table.ConfFlowNode;
import com.github.jspxnet.worktask.table.ConfWorkFlow;
import com.github.jspxnet.worktask.util.WorkFlowUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jspx.net
 * author: chenYuan
 * date: 2021/9/27 23:16
 * description: jspx-framework
 **/
public class ConfFlowView extends ActionSupport {

    @Ref
    protected ConfWorkFlowDAO confWorkFlowDAO;
    /**
     * @param params 模版参数
     * @return 返回模版列表
     */
    @Operate(caption = "得到", method = "list/page")
    public RocResponse<List<ConfWorkFlowDto>> getListPage(@Param(caption = "请求参数", required = true) PageParam params) {
        long totalCount = confWorkFlowDAO.getCount(params.getField(), params.getFind(),params.getNamespace(),params.getTerm(), params.getUid());
        if (totalCount <= 0) {
            return RocResponse.success(new ArrayList<>(), language.getLang(LanguageRes.notDataFind));
        }
        List<ConfWorkFlow> list = confWorkFlowDAO.getList(params.getField(), params.getFind(),params.getNamespace(),
                params.getTerm(),   params.getUid(),params.getSort(),params.getCurrentPage(), params.getCount());
        List<ConfWorkFlowDto> result = BeanUtil.copyList(list, ConfWorkFlowDto.class);
        return RocResponse.success(result, params, totalCount);
    }

    @Operate(caption = "得到详细", method = "detail/${id}",post = false)
    public RocResponse<ConfWorkFlowDto> detail(@PathVar(caption = "id",name = "id") Long id) {
        ConfWorkFlow confWorkFlow =  confWorkFlowDAO.load(id);
        return RocResponse.success(WorkFlowUtil.convertDto(confWorkFlow));
    }

    @Operate(caption = "流程编号流程配置", method = "flow/${namespace}/${flowNo}",post = false)
    public RocResponse<ConfWorkFlowDto> getConfWorkFlow(@PathVar(caption = "流程编号",min = 1,max = 32,  message = "流程编号不能为空") String flowNo,
                                       @PathVar(caption = "命名空间",min = 1,max = 32, message = "命名空间不能为空") String namespace) {

        ConfWorkFlow confWorkFlow = confWorkFlowDAO.getByFlowNo(flowNo,namespace);
        return RocResponse.success(WorkFlowUtil.convertDto(confWorkFlow));
    }
}
